﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data; 
using System.Data.Common;
using WMSADBUtility;
using WMSAML.SY;

namespace WMSADL.SY
{
    public class TRolePermissionRelation_DAL
    {
        /// <summary>
        /// 
        /// </summary>
        public int Add(TRolePermissionRelation_Entity entity)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into TRolePermissionRelation(");
            strSql.Append("Role_id,Permission_id)");

            strSql.Append(" values (");
            strSql.Append("@Role_id,@Permission_id)");
            strSql.Append(";select @@IDENTITY");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "Role_id", DbType.Int32, entity.Role_id);
            db.AddInParameter(dbCommand, "Permission_id", DbType.String, entity.Permission_id);
            int result;
            object obj = db.ExecuteScalar(dbCommand);
            if (!int.TryParse(obj.ToString(), out result))
            {
                return 0;
            }
            return result;
        }
        /// <summary>
        /// 删除
        /// </summary>
        public bool Delete(int id)
        {
            Database db = DatabaseFactory.CreateDatabase();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("DELETE  FROM TRolePermissionRelation WHERE T_RolePermission_id=@T_RolePermission_id ");
            using (DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString()))
            {
                db.AddInParameter(dbCommand, "T_RolePermission_id", DbType.Int32, id);
                int cmdresult;
                object obj = db.ExecuteScalar(dbCommand);
                if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
                {
                    cmdresult = 0;
                }
                else
                {
                    cmdresult = int.Parse(obj.ToString());
                }
                if (cmdresult == 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(TRolePermissionRelation_Entity entity)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("UPDATE TRolePermissionRelation SET ");
            strSql.Append("Role_id=@Role_id,");
            strSql.Append("Permission_id=@Permission_id");
            strSql.Append(" WHERE Role_id=@Role_id ");
            Database db = DatabaseFactory.CreateDatabase();
            using (DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString()))
            {
                db.AddInParameter(dbCommand, "T_RolePermission_id", DbType.Int32, entity.T_RolePermission_id);
                db.AddInParameter(dbCommand, "Role_id", DbType.Int32, entity.Role_id);
                db.AddInParameter(dbCommand, "Permission_id", DbType.String, entity.Permission_id);
                int rows = db.ExecuteNonQuery(dbCommand);

                if (rows > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        /// <summary>
        /// 角色是否存在
        /// </summary>
        public bool ExistsByID(int rid)
        {
            Database db = DatabaseFactory.CreateDatabase();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT COUNT(1) FROM TRolePermissionRelation WHERE Role_id=@Role_id ");
            using (DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString()))
            {
                db.AddInParameter(dbCommand, "Role_id", DbType.Int32, rid);
                int cmdresult;
                object obj = db.ExecuteScalar(dbCommand);
                if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
                {
                    cmdresult = 0;
                }
                else
                {
                    cmdresult = int.Parse(obj.ToString());
                }
                if (cmdresult == 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
        }

        /// <summary>
        /// ByID
        /// </summary>
        /// <summary>
        public TRolePermissionRelation_Entity GetEntityByID(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select T_RolePermission_id,Role_id,Permission_id from TRolePermissionRelation ");
            strSql.Append(" where T_RolePermission_id=@T_RolePermission_id ");
            Database db = DatabaseFactory.CreateDatabase();
            using (DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString()))
            {
                db.AddInParameter(dbCommand, "T_RolePermission_id", DbType.Int32, id);
                TRolePermissionRelation_Entity entity = new TRolePermissionRelation_Entity();
                using (IDataReader dataReader = db.ExecuteReader(dbCommand))
                {
                    if (dataReader.Read())
                    {
                        entity.T_RolePermission_id = Convert.ToInt32(dataReader["T_RolePermission_id"].ToString());
                        entity.Role_id = Convert.ToInt32(dataReader["Role_id"].ToString());
                        entity.Permission_id = dataReader["Permission_id"].ToString();
                    }
                }
                return entity;
            }
        }
    }
}
